using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

public class ResSvc : ILogic
{
    private AsyncOperation mAo;
    private Action mUpdateProcessAction;
    private readonly Dictionary<string,GameObject> mPrefabDic =  new Dictionary<string, GameObject>();

    public void Init()
    {
        this.Log("ResSvc Init...");
    }

    public void Tick()
    {
        mUpdateProcessAction?.Invoke();
    }

    public void UnInit()
    {
        this.Log("ResSvc UnInit...");
    }

    /// <summary>
    /// 资源加载
    /// </summary>
    /// <param name="assetName"></param>
    /// <param name="cache"></param>
    /// <returns></returns>
    public GameObject LoadAsset(string assetName,bool cache = true)
    {
        GameObject go = null;
        if (mPrefabDic.ContainsKey(assetName))
        {
            go = mPrefabDic[assetName];
        }
        else
        {
           GameObject temp = Resources.Load<GameObject>(assetName);
           go = Object.Instantiate(temp);
           if (cache)
           {
               if (!mPrefabDic.ContainsKey(assetName))
               {
                   mPrefabDic.Add(assetName, go);
               }
           }
        }
        return go;
    }

    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="loadProcessAction">加载进度回调</param>
    /// <param name="loadDone">加载完成回调</param>
    public void LoadSceneAsync(string sceneName, Action<float> loadProcessAction, Action loadDone)
    {
        if (sceneName == SceneManager.GetActiveScene().name)
        {
            loadProcessAction?.Invoke(1f);
            loadDone?.Invoke();
        }
        else
        {
            mAo = SceneManager.LoadSceneAsync(sceneName);
            mUpdateProcessAction = () =>
            {
                if (mAo.isDone)
                {
                    loadDone?.Invoke();
                    mUpdateProcessAction = null;
                    mAo = null;
                }
                else
                {
                    loadProcessAction?.Invoke(mAo.progress);
                }
            };
        }
    }

    private Dictionary<string, AudioClip> mAudioDic = new Dictionary<string, AudioClip>();
    /// <summary>
    /// 加载音频
    /// </summary>
    /// <param name="clipName"></param>
    /// <returns></returns>
    public AudioClip LoadClip(string clipName,bool cache =true)
    {
        if (!mAudioDic.TryGetValue(clipName,out AudioClip clip))
        {
            clip = Resources.Load<AudioClip>(clipName);
            if (cache)
            {
                mAudioDic[clipName] = clip;
            }
        }

        return clip;

    }

    private Dictionary<string,Sprite> mSpriteDic = new Dictionary<string, Sprite>();
    /// <summary>
    /// 加载2D图片
    /// </summary>
    /// <param name="spriteName"></param>
    /// <param name="cache">是否缓存</param>
    /// <returns></returns>
    public Sprite LoadSprite(string spriteName, bool cache = true)
    {
        if (!mSpriteDic.TryGetValue(spriteName, out Sprite sprite))
        {
            sprite = Resources.Load<Sprite>(spriteName);
            if (cache)
            {
                mSpriteDic[spriteName] = sprite;
            }
        }
        return sprite;
    }
}